﻿<?xml version="1.0" encoding="utf-8" ?>

<cls:class
	name="ParallelPortReader"
	type="http://metalx.org/Ports/Parallel/ParallelPortReader"
	xmlns="http://metalx.org/Program"
	xmlns:mm="http://metalx.org/i386/Functions/MemoryManager"
	xmlns:dbg="http://metalx.org/i386/Functions/Debug"
	xmlns:bool="http://metalx.org/i386/Functions/Boolean"
	xmlns:byte="http://metalx.org/i386/Functions/Byte"
	xmlns:short="http://metalx.org/i386/Functions/Short"
	xmlns:int="http://metalx.org/i386/Functions/Integer"
	xmlns:hex="http://metalx.org/i386/Functions/Hexadecimal"
	xmlns:float="http://metalx.org/i386/Functions/Float"
	xmlns:dbl="http://metalx.org/i386/Functions/Double"
	xmlns:real="http://metalx.org/i386/Functions/Real"
	xmlns:lst="http://metalx.org/i386/Functions/List"
	xmlns:loop="http://metalx.org/i386/Functions/Loop"
	xmlns:con="http://metalx.org/i386/Functions/Console"
	xmlns:str="http://metalx.org/i386/Functions/String"
	xmlns:clsf="http://metalx.org/i386/Functions/Class"
	xmlns:var="http://metalx.org/Variable"
	xmlns:sys="http://metalx.org/i386/Functions/System"
	xmlns:cat="http://metalx.org/i386/Functions/Catalog"
	xmlns:obj="http://metalx.org/i386/Functions/Object"
	xmlns:asc="http://metalx.org/Ansi/Ascii"
	xmlns:fpu="http://metalx.org/Intel/80386/FpuOperators"
	xmlns:logic="http://metalx.org/Intel/80386/Logic"
	xmlns:math="http://metalx.org/Intel/80386/Math"
	xmlns:shift="http://metalx.org/Intel/80386/Shift"
	xmlns:imm="http://metalx.org/Intel/80386/Immediate"
	xmlns:inc="http://metalx.org/Intel/80386/Increment"
	xmlns:cpu="http://metalx.org/Intel/80386/Operators"
	xmlns:op="http://metalx.org/Intel/80386/Operands"
	xmlns:cls="http://metalx.org/Class">

	<cls:field name="Port" type="http://metalx.org/Hexadecimal" offset="0"/>

	<cls:method name="Create" type="http://metalx.org/Ports/Parallel/ParallelPortReader" static="true">
		<cls:parameter name="Port" type="http://metalx.org/Hexadecimal"/>

		<cpu:ExchangeOperandWithRegister/>
		<op:BP-SPRegister/>

		<cpu:PullSIFromStack/>

		<cpu:ExchangeOperandWithRegister/>
		<op:BP-SPRegister/>

		<obj:GetData/>

		<cpu:CopySIAddressToAXAndIncrementSI/>

		<mm:CheckOut length="4"/>

		<cpu:CopyRegisterToOperand/>
		<op:AX-DIAddress/>

		<obj:Create length="4"/>

		<cpu:ReturnToNearCaller/>
	</cls:method>
	
	<cls:method name="Skip" type="metalx.org/Ports/Parallel/ParallelPortReader">
		<cls:parameter name="Count" type="http://metalx.org/Integer"/>

		<cpu:PushDIToStack/>

		<cpu:CopyRegisterToOperand/>
		<op:DI-SIRegister/>

		<obj:GetData/>

		<cpu:CopyOperandToRegister/>
		<op:DX-SIAddress/>

		<cpu:ExchangeOperandWithRegister/>
		<op:BP-SPRegister/>

		<cpu:PullSIFromStack/>
		
		<cpu:ExchangeOperandWithRegister/>
		<op:BP-SPRegister/>

		<obj:GetData/>

		<cpu:CopyOperandToRegister/>
		<op:CX-SIAddress/>

		<cpu:Repeat/>
		<cpu:ReadFromDXPortToAL/>

		<cpu:ReturnToNearCaller/>
	</cls:method>

	<cls:method name="SetPosition" type="http://metalx.org/Ports/PortReader">
		<cls:parameter name="Position" type="http://metalx.org/Hexadecimal"/>

		<cpu:MathImmediate8ToOperandFunction/>
		<math:AddToBPRegister/>
		<byte>4</byte>

		<cpu:ReturnToNearCaller/>
	</cls:method>

	<cls:method name="ReadBytes" type="http://metalx.org/List">
		<cls:parameter name="Count" type="http://metalx.org/Integer"/>

		<cpu:CopyImmediateToDI/>
		<int>0</int>

		<cpu:ReturnToNearCaller/>
	</cls:method>

	<cls:method name="ReadBoolean" type="http://metalx.org/Boolean">
		<!--Get Port-->
		<cpu:CopyRegisterToOperand/>
		<op:DI-SIRegister/>

		<obj:GetData/>

		<cpu:CopyOperandToRegister/>
		<op:DX-SIAddress/>

		<cpu:CallRelative/>
		<addressOf ref="readByte" type="Relative32"/>

		<byte:CreateObject/>

		<cpu:ReturnToNearCaller/>
	</cls:method>

	<cls:method name="ReadByte" type="http://metalx.org/Byte">
		<!--Get Port-->
		<cpu:CopyRegisterToOperand/>
		<op:DI-SIRegister/>

		<obj:GetData/>

		<cpu:CopyOperandToRegister/>
		<op:DX-SIAddress/>

		<cpu:CallRelative/>
		<addressOf ref="readByte" type="Relative32"/>

		<byte:CreateObject/>

		<cpu:ReturnToNearCaller/>
	</cls:method>

	<cls:method name="ReadShort" type="http://metalx.org/Short">
		<!--Get Port-->
		<cpu:CopyRegisterToOperand/>
		<op:DI-SIRegister/>

		<obj:GetData/>

		<cpu:CopyOperandToRegister/>
		<op:DX-SIAddress/>

		<cpu:CallRelative/>
		<addressOf ref="readByte" type="Relative32"/>

		<cpu:Operand16/>
		<cpu:ShiftRegisterByImmediate8Function/>
		<shift:RollAXRegisterRight/>
		<byte>8</byte>

		<cpu:CallRelative/>
		<addressOf ref="readByte" type="Relative32"/>

		<cpu:Operand16/>
		<cpu:ShiftRegisterByImmediate8Function/>
		<shift:RollAXRegisterRight/>
		<byte>8</byte>

		<short:CreateObject/>

		<cpu:ReturnToNearCaller/>
	</cls:method>

	<cls:method name="ReadInteger" type="http://metalx.org/Integer">
		<!--Get Port-->
		<cpu:CopyRegisterToOperand/>
		<op:DI-SIRegister/>

		<obj:GetData/>

		<cpu:CopyOperandToRegister/>
		<op:DX-SIAddress/>

		<cpu:CallRelative/>
		<addressOf ref="readByte" type="Relative32"/>

		<cpu:ShiftRegisterByImmediate8Function/>
		<shift:RollAXRegisterRight/>
		<byte>8</byte>

		<cpu:CallRelative/>
		<addressOf ref="readByte" type="Relative32"/>

		<cpu:ShiftRegisterByImmediate8Function/>
		<shift:RollAXRegisterRight/>
		<byte>8</byte>
		
		<cpu:CallRelative/>
		<addressOf ref="readByte" type="Relative32"/>

		<cpu:ShiftRegisterByImmediate8Function/>
		<shift:RollAXRegisterRight/>
		<byte>8</byte>

		<cpu:CallRelative/>
		<addressOf ref="readByte" type="Relative32"/>

		<cpu:ShiftRegisterByImmediate8Function/>
		<shift:RollAXRegisterRight/>
		<byte>8</byte>

		<int:CreateObject/>

		<cpu:ReturnToNearCaller/>
	</cls:method>

	<cls:method name="ReadHexadecimal" type="http://metalx.org/Hexadecimal">
		<!--Get Port-->
		<cpu:CopyRegisterToOperand/>
		<op:DI-SIRegister/>

		<obj:GetData/>

		<cpu:CopyOperandToRegister/>
		<op:DX-SIAddress/>

		<cpu:CallRelative/>
		<addressOf ref="readByte" type="Relative32"/>

		<cpu:ShiftRegisterByImmediate8Function/>
		<shift:RollAXRegisterRight/>
		<byte>8</byte>

		<cpu:CallRelative/>
		<addressOf ref="readByte" type="Relative32"/>

		<cpu:ShiftRegisterByImmediate8Function/>
		<shift:RollAXRegisterRight/>
		<byte>8</byte>

		<cpu:CallRelative/>
		<addressOf ref="readByte" type="Relative32"/>

		<cpu:ShiftRegisterByImmediate8Function/>
		<shift:RollAXRegisterRight/>
		<byte>8</byte>

		<cpu:CallRelative/>
		<addressOf ref="readByte" type="Relative32"/>

		<cpu:ShiftRegisterByImmediate8Function/>
		<shift:RollAXRegisterRight/>
		<byte>8</byte>

		<hex:CreateObject/>

		<cpu:ReturnToNearCaller/>
	</cls:method>

	<cls:method name="ReadFloat" type="http://metalx.org/Float">
		<cpu:ReturnToNearCaller/>
	</cls:method>

	<cls:method name="ReadDouble" type="http://metalx.org/Double">
		<cpu:ReturnToNearCaller/>
	</cls:method>

	<cls:method name="ReadReal" type="http://metalx.org/Real">
		<cpu:ReturnToNearCaller/>
	</cls:method>

	<cls:method name="ReadString" type="http://metalx.org/String">
		<cls:parameter name="Count" type="http://metalx.org/Integer"/>

		<!--Get Port-->
		<cpu:CopyRegisterToOperand/>
		<op:DI-SIRegister/>

		<obj:GetData/>

		<cpu:CopyOperandToRegister/>
		<op:DX-SIAddress/>

		<!--Get Count-->
		<cpu:CopyOperandToRegister/>
		<op:SI-BPAddressPlusImmediate8/>
		<byte>0</byte>

		<cpu:MathImmediate8ToOperandFunction/>
		<math:AddToBPRegister/>
		<byte>4</byte>

		<obj:GetData/>

		<cpu:CopyOperandToRegister/>
		<op:CX-SIAddress/>

		<!--Create String-->
		<cpu:CopyRegisterToOperand/>
		<op:CX-AXRegister/>

		<str:Create/>

		<loop:For>
			<cpu:CallRelative/>
			<addressOf ref="readByte" type="Relative32"/>

			<cpu:CopyRegisterToOperand/>
			<op:DI-SIRegister/>

			<str:AppendCharacter/>
		</loop:For>

		<str:CreateObject/>

		<cpu:ReturnToNearCaller/>
	</cls:method>

	<cls:method name="ReadObject" type="http://metalx.org/Ports/Parallel/ParallelPortReader">
		<cls:parameter name="Object" type="http://metalx.org/Object"/>

		<cpu:PushDIToStack/>
		
		<!--Get Port-->
		<cpu:CopyRegisterToOperand/>
		<op:DI-SIRegister/>

		<obj:GetData/>

		<cpu:CopyOperandToRegister/>
		<op:DX-SIAddress/>

		<!--Get Count-->
		<cpu:CopyOperandToRegister/>
		<op:SI-BPAddressPlusImmediate8/>
		<byte>0</byte>

		<cpu:MathImmediate8ToOperandFunction/>
		<math:AddToBPRegister/>
		<byte>4</byte>

		<obj:GetDataLength/>

		<obj:GetData/>

		<cpu:CopyRegisterToOperand/>
		<op:AX-CXRegister/>

		<cpu:CopyRegisterToOperand/>
		<op:SI-DIRegister/>

		<loop:For>
			<cpu:CallRelative/>
			<addressOf ref="readByte" type="Relative32"/>

			<cpu:CopyALToDIAddressAndIncrementDI/>
		</loop:For>

		<cpu:PullDIFromStack/>

		<cpu:ReturnToNearCaller/>
	</cls:method>

	<cls:method name="ToString" type="http://metalx.org/String">
		<cpu:CopyImmediateToDI/>
		<addressOf ref="className" type="Absolute32"/>

		<str:CreateObject/>

		<cpu:ReturnToNearCaller/>

		<var:string id="className">Parallel Port Reader</var:string>
	</cls:method>

	<cls:method name="GetClass" type="http://metalx.org/Class">
		<cpu:CopyRegisterToOperand/>
		<op:DI-SIRegister/>

		<obj:GetType/>

		<cpu:CopyRegisterToOperand/>
		<op:SI-DIRegister/>

		<clsf:CreateObject/>

		<cpu:ReturnToNearCaller/>
	</cls:method>

	<cls:method name="GetClass" type="http://metalx.org/Class" static="true">
		<cpu:CopyRegisterToOperand/>
		<op:BX-DIRegister/>

		<clsf:CreateObject/>

		<cpu:ReturnToNearCaller/>
	</cls:method>

	<label id="readByte">
		<!--Wait For Ready-->
		<cpu:IncrementDX/>

		<label id="waitForBusy"/>

		<cpu:ReadFromDXPortToAL/>

		<cpu:TestALWithImmediate/>
		<binary>00000001</binary>

		<!--<cpu:BranchToRelative8IfNotZero/>
		<addressOf ref="waitForBusy" type="Relative8"/>-->

		<!--Read Byte-->
		<cpu:DecrementDX/>

		<cpu:ReadFromDXPortToAL/>

		<cpu:ReturnToNearCaller/>
	</label>
</cls:class>